package com.fzhucc.modules.displayMap.rest;


import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.displayMap.domain.DisplayMap;
import com.fzhucc.modules.displayMap.domain.dto.DisplayMapAddEditDto;
import com.fzhucc.modules.displayMap.domain.dto.DisplayMapQueryCriteria;
import com.fzhucc.modules.displayMap.domain.dto.DisplayMapTypeQueryCriteria;
import com.fzhucc.modules.displayMap.service.DisplayMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yyg
 * @date 2022-10-25
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "闽侯：地图展示管理")
@RequestMapping("/api/displayMap")
public class DisplayMapController {

    private final DisplayMapService displayMapService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('displayMap:list')")
    public void exportDisplayMap(HttpServletResponse response, DisplayMapQueryCriteria criteria) throws IOException {
        displayMapService.download(displayMapService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询displayMap")
    @ApiOperation("查询displayMap")
    //@PreAuthorize("@el.check('displayMap:list')")
    @AnonymousAccess
    public ResponseEntity<Object> queryDisplayMap(DisplayMapQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(displayMapService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增displayMap")
    @ApiOperation("新增displayMap")
    @PreAuthorize("@el.check('displayMap:add')")
    public ResponseEntity<Object> createDisplayMap(@Validated @RequestBody DisplayMapAddEditDto resources){
        return new ResponseEntity<>(displayMapService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改displayMap")
    @ApiOperation("修改displayMap")
    @PreAuthorize("@el.check('displayMap:edit')")
    public ResponseEntity<Object> updateDisplayMap(@Validated @RequestBody DisplayMapAddEditDto resources){
        displayMapService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除displayMap")
    @ApiOperation("删除displayMap")
    @PreAuthorize("@el.check('displayMap:del')")
    public ResponseEntity<Object> deleteDisplayMap(@RequestBody Integer[] ids) {
        displayMapService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/getMapType")
    @Log("获取地图分类")
    @ApiOperation("获取地图分类")
    @AnonymousAccess
    public ResponseEntity<Object> getMapType(DisplayMapTypeQueryCriteria criteria) {
        return new ResponseEntity<>(displayMapService.findAllType(criteria),HttpStatus.OK);
    }
}